package com.liy.supercloud.ssoclient.interceptor;

import com.liy.framework.common.utils.ContextHandler;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Configuration
public class FeignInterceptorService implements RequestInterceptor {

    private Logger log = LoggerFactory.getLogger(FeignInterceptorService.class);

    @Value("${dascloud.authrization.enabled: true}")
    private Boolean enabled;

    @Value("${dascloud.authrization.token-header:JwtToken}")
    private String tokenHeader;

    @Value("${dascloud.authrization.auth-key:AuthKey}")
    private String authKeyHeader;


    public void apply(RequestTemplate requestTemplate) {
        if (this.enabled) {
            this.log.debug("Add authentication key to feign request: " + this.tokenHeader + ":" + this.authKeyHeader);
            this.log.debug(ContextHandler.getJwtToken());
            requestTemplate.header(this.tokenHeader, new String[]{ContextHandler.getJwtToken()});
            requestTemplate.header(this.authKeyHeader, new String[]{ContextHandler.getAuthKey()});
        }
    }

    private HttpServletRequest getHttpServletRequest() {
        try {
            return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
        } catch (Exception var2) {
            var2.printStackTrace();
            return null;
        }
    }

    private Map<String, String> getHeaders(HttpServletRequest request) {
        Map<String, String> map = new LinkedHashMap();
        Enumeration enumeration = request.getHeaderNames();

        while(enumeration.hasMoreElements()) {
            String key = (String)enumeration.nextElement();
            String value = request.getHeader(key);
            map.put(key, value);
        }

        return map;
    }
}
